package com.csnj.admin.controller.SystemManager;

import com.csnj.common.entity.APIResult;
import com.csnj.common.rest.BaseController;
import com.csnj.common.utils.TreeUtil;
import com.csnj.core.Srvs.SysManager.SysRoleSrvs;
import com.csnj.core.constant.CoreConstants;
import com.csnj.core.entity.SysRole;
import com.csnj.core.entity.VO.GroupTree;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController extends BaseController<SysRoleSrvs, SysRole> {


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public APIResult list(String name, String groupType) {
        if(StringUtils.isBlank(name)&&StringUtils.isBlank(groupType)) {
            return new APIResult(new ArrayList<SysRole>());
        }
        Example example = new Example(SysRole.class);
        if (StringUtils.isNotBlank(name)) {
            example.createCriteria().andLike("name", "%" + name + "%");
        }
        if (StringUtils.isNotBlank(groupType)) {
            example.createCriteria().andEqualTo("groupType", groupType);
        }

        return new APIResult(baseBiz.selectByExample(example));
    }



    @RequestMapping(value = "/{id}/user", method = RequestMethod.PUT)
    public APIResult modifiyUsers(@PathVariable int id,String members,String leaders){
        baseBiz.modifyGroupUsers(id, members, leaders);
        return new APIResult("OK");
    }

    @RequestMapping(value = "/{id}/user", method = RequestMethod.GET)
    public APIResult getUsers(@PathVariable int id){
        return new APIResult(baseBiz.getGroupUsers(id));
    }

    @RequestMapping(value = "/{id}/authority/menu", method = RequestMethod.POST)
    public APIResult modifyMenuAuthority(@PathVariable  int id, String menuTrees){
        String [] menus = menuTrees.split(",");
        Integer [] menuIds = new Integer[menus.length];
        for(int i=0;i<menus.length;i++) {
            menuIds[i] = Integer.valueOf(menus[i]);
        }
        baseBiz.modifyAuthorityMenu(id, menuIds);
        return new APIResult("OK");
    }

    @RequestMapping(value = "/{id}/authority/menu", method = RequestMethod.GET)
    public APIResult getMenuAuthority(@PathVariable  int id){
        return new APIResult(baseBiz.getAuthorityMenu(id));
    }

    @RequestMapping(value = "/{id}/authority/element/add", method = RequestMethod.POST)
    public APIResult addElementAuthority(@PathVariable  int id,int menuId, int elementId){
        baseBiz.modifyAuthorityElement(id,menuId,elementId);
        return new APIResult("OK");
    }

    @RequestMapping(value = "/{id}/authority/element/remove", method = RequestMethod.POST)
    public APIResult removeElementAuthority(@PathVariable int id,int menuId, int elementId){
        baseBiz.removeAuthorityElement(id,menuId,elementId);
        return new APIResult("OK");
    }

    @RequestMapping(value = "/{id}/authority/element", method = RequestMethod.GET)
    public APIResult getElementAuthority(@PathVariable  int id){
        return new APIResult(baseBiz.getAuthorityElement(id));
    }


    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseBody
    public APIResult tree(String name,String groupType) {
        if(StringUtils.isBlank(name)&&StringUtils.isBlank(groupType)) {
            return new APIResult(new ArrayList<>());
        }
        Example example = new Example(SysRole.class);
        if (StringUtils.isNotBlank(name)) {
            example.createCriteria().andLike("name", "%" + name + "%");
        }
        if (StringUtils.isNotBlank(groupType)) {
            example.createCriteria().andEqualTo("groupType", groupType);
        }
        return  new APIResult(getTree(baseBiz.selectByExample(example), CoreConstants.ROOT));
    }


    private List<GroupTree> getTree(List<SysRole> groups,int root) {
        List<GroupTree> trees = new ArrayList<GroupTree>();
        GroupTree node = null;
        for (SysRole group : groups) {
            node = new GroupTree();
            node.setLabel(group.getName());
            BeanUtils.copyProperties(group, node);
            trees.add(node);
        }
        return TreeUtil.bulid(trees,root) ;
    }
}
